#include "masterGetCycleSensor.h"
#include <stdlib.h>

#define GET_REQ_MSG 0 //placeholder da modificare
#define GET_ACK_P 1 //placeholder da modificare
#define GET_START_MSG 2 //placeholder da modificare
#define GET_PACK 3 //placeholder da modificare
#define GET_ACK 4 //placeholder da modificare
#define GET_END 5 //placeholder da modificare

static Event onNotifyMasterStartPacket={ON_NOTIFY_MASTER_SENSOR_START_PACKET,NULL};
static Event onNotifyMasterSensorAckPacket={ON_NOTIFY_MASTER_SENSOR_ACK_PACKET,NULL};
static Event onNotifyMasterSensorFail={ON_NOTIFY_MASTER_SENSOR_FAIL,NULL};


void NotifyMasterCycleGetSensorFail(void* target){
	stateMachineDispatcher(target,&onNotifyMasterSensorFail);
}
void NotifyMasterCycleGetSensorGetAckPacket(void* target){
	stateMachineDispatcher(target,&onNotifyMasterSensorAckPacket);
}
void NotifyMasterCycleGetSensorStartPacket(void* target){
	stateMachineDispatcher(target,&onNotifyMasterStartPacket);
}
void fromSensorGetPacketSendingStateToSensorGetPacketSendingState(void* me){
	MasterMachine* temp=(MasterMachine*)me;
	temp->i++;
	temp->pCounter--;
}
void fromSensorGetInitStateToSensorGetPacketSendingState(void* me){
	MasterMachine* temp=(MasterMachine*)me;
	temp->i=0;
}
StateOutcome masterGetCycleSensorFinishFailState (void* me,const Event* e){
	return EVENT_IGNORED; //da arricchire/sostituire con gestione crash comunicazione
}
StateOutcome masterGetCycleSensorInitTransactionState (void* me,const Event* e){
	MasterMachine* temp=(MasterMachine*)me;
	initSensorGetData* eventParser;
	switch(e->signature){
	case INIT_CODE:
	case ENTRY_CODE:
		eventParser=(initSensorGetData*)(e->data);
		temp->pCounter=eventParser->pPacket;
		temp->masterBuffer=eventParser->contentBuffer;
		masterSensorGetCycleHostInit(me,NULL,GET_ACK_P,2,(uint8_t*)&(temp->pCounter),GET_START_MSG,NotifyMasterCycleGetSensorStartPacket);
		return EVENT_HANDLED;
	case ON_NOTIFY_MASTER_SENSOR_START_PACKET:
		temp->super.passageAction=fromSensorGetInitStateToSensorGetPacketSendingState;
		temp->super.state=masterGetCycleSensorPacketSendingState;
		return EVENT_CHANGED;
	case ON_NOTIFY_MASTER_SENSOR_FAIL:
		temp->super.passageAction=NULL;
		temp->super.state=masterGetCycleSensorFinishFailState;
		return EVENT_CHANGED;
	default:
		return EVENT_IGNORED;
	}
}
StateOutcome masterGetCycleSensorPacketSendingState (void* me,const Event* e){
	MasterMachine* temp=(MasterMachine*)me;
	uint8_t* packetSent;
	uint8_t packetSize;
	switch(e->signature){
	case ENTRY_CODE:
		packetSize=packetBuilder(temp->masterBuffer,temp->i,&packetSent);
		masterSensorGetCycleHostInit(me,NULL,GET_PACK,packetSize,packetSent,GET_ACK,NotifyMasterCycleGetSensorGetAckPacket);
		return EVENT_HANDLED;
	case ON_NOTIFY_MASTER_SENSOR_ACK_PACKET:
		if (temp->pCounter==1){ //gestione guardia
			temp->super.passageAction=NULL;
			temp->super.state=masterGetCycleSensorEndTransactionState; //altrimenti lascia inalterato lo stato di destinazione
		}
		else
			temp->super.passageAction=fromSensorGetPacketSendingStateToSensorGetPacketSendingState;
		return EVENT_CHANGED;
	case ON_NOTIFY_MASTER_SENSOR_FAIL:
		temp->super.passageAction=NULL;
		temp->super.state=masterGetCycleSensorFinishFailState;
		return EVENT_CHANGED;
	default:
		return EVENT_IGNORED;
	}
}
StateOutcome masterGetCycleSensorEndTransactionState (void* me,const Event* e){
	//MasterMachine* temp=(MasterMachine*)me; //decommentare all'occorrenza
	switch(e->signature){
	case ENTRY_CODE:
		//invia end msg non protetto
		return EVENT_HANDLED;
	default:
		return EVENT_IGNORED; //fine: se si vuol mandare altro bisogna reinizializzare la macchina
	}
}
void masterSensorGetCycleHostInit(void* me,void* canPort,uint32_t msgId, uint8_t msgPayloadSize, uint8_t* msgPayload, uint32_t msgAnswer, Notifier onOk){
	MasterMachine* temp=(MasterMachine*)me;
	initTransmission(&temp->microCycleHandler,canPort,msgId,msgPayloadSize,msgPayload,msgAnswer, me, onOk, NotifyMasterCycleGetSensorFail);
}
